$folderName = "java"
# 设置项目目录路径、下载链接和文件夹名称
$projectDir = "."
$downloadUrl = "http://localhost:3000/download-devcontainer/$folderName"
$devcontainerZip = "$projectDir/devcontainer.zip"
$devcontainerDir = "$projectDir/.devcontainer"

function ConvertTo-Hex {
    param ([string]$inputString)
    $hexString = ""
    foreach ($char in $inputString.ToCharArray()) {
        $hexString += [convert]::ToString([int][char]$char, 16)
    }
    return $hexString
}

# 将十六进制字符串转换回原字符串
function ConvertFrom-Hex {
    param ([string]$hexString)
    $outputString = ""
    for ($i = 0; $i -lt $hexString.Length; $i += 2) {
        $outputString += [char][convert]::ToInt32($hexString.Substring($i, 2), 16)
    }
    return $outputString
}


# 启动开发容器并捕获输出
$output = devcontainer up --workspace-folder .

# 解析 JSON 输出并提取 Container ID
try {
    $json = $output | ConvertFrom-Json
    $containerId = $json.containerId

    # 截取 Container ID 的前12位
    $shortContainerId = $containerId.Substring(0, 12)

    # 打印截取后的 Container ID
    Write-Output "Short Container ID: $shortContainerId"

    # 将容器 ID 转换为十六进制表示
    $hexContainerId = ConvertTo-Hex $shortContainerId
    
    # 使用 VSCode 连接到容器
    Write-Output "使用 VSCode 连接到容器..."
    code --folder-uri "vscode-remote://attached-container+$hexContainerId/"
} catch {
    Write-Error "Failed to parse container output or connect to VSCode. Please check the devcontainer setup."
    exit 1
}


